﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class frm_DocGia : DevExpress.XtraEditors.XtraForm
    {
        public frm_DocGia()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaDocGia;

        public int MaDocGia
        {
            get { return _MaDocGia; }
            set { _MaDocGia = value; }
        }
        private string _HoVaTen;

        public string HoVaTen
        {
            get { return _HoVaTen; }
            set { _HoVaTen = value; }
        }
        private DateTime _NgaySinh;

        public DateTime NgaySinh
        {
            get { return _NgaySinh; }
            set { _NgaySinh = value; }
        }
        private string _GioiTinh;

        public string GioiTinh
        {
            get { return _GioiTinh; }
            set { _GioiTinh = value; }
        }
        private string _DiaChi;

        public string DiaChi
        {
            get { return _DiaChi; }
            set { _DiaChi = value; }
        }
        private string _TenLop;

        public string TenLop
        {
            get { return _TenLop; }
            set { _TenLop = value; }
        }
        private string _DienThoai;

        public string DienThoai
        {
            get { return _DienThoai; }
            set { _DienThoai = value; }
        }
        private string _Email;

        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
        private DateTime _NgayCap;

        public DateTime NgayCap
        {
            get { return _NgayCap; }
            set { _NgayCap = value; }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void EnableTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled =false;
            }
            foreach (Control i in ctr.Controls)
                EnableTextFalse(i);
        }
        public void GetData()
        {
            txt_MaDG.Text = MaDocGia.ToString();
            txt_HoTen.Text = HoVaTen;
            dateTime_NgaySinh.Text = NgaySinh.ToShortDateString();
            cob_GioiTinh.Text = GioiTinh;
            txt_DiaChi.Text = DiaChi;
            cob_TenLop.Text = TenLop;
            txt_DienThoai.Text = DienThoai;
            txt_Email.Text = Email;
            dateTime_NgayCap.Text = NgayCap.ToShortDateString();
            //
            DataTable dt = new DataTable();
            string sql = "select * from tbl_Lop,tbl_DocGia where tbl_Lop.MaLop=tbl_DocGia.MaLop and tbl_DocGia.MaDocGia='"+txt_MaDG.Text+"'";
            dt = docgia_bll.TaoBang(sql);
            cob_MaLop.DataSource = dt;
            cob_MaLop.DisplayMember = "MaLop";
            cob_TenLop.DataSource = dt;
            cob_TenLop.DisplayMember = "TenLop";
        }
        public void GetMa(int ma)
        {
            cob_MaLop.Text = ma.ToString();
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Độc Giả";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Độc Giả";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Độc Giả";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                bbi_Luu.Enabled = false;
                EnableTextFalse(this);
                GetData();
            }
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        DocGia_BLL docgia_bll = new DocGia_BLL();
        private void frm_DocGia_Load(object sender, EventArgs e)
        {
            LoadForm();
            cob_MaLop.Hide();
            bbi_Dau.Enabled = false;
            bbi_Sau.Enabled = false;
            bbi_Tiep.Enabled = false;
            bbi_CuoiCung.Enabled = false;
        }

        private void btn_Lop_Click(object sender, EventArgs e)
        {
            frm_DocGia_Lop f = new frm_DocGia_Lop();
            f.MyGetData = new frm_DocGia_Lop.GetInt(GetMa);
            f.ShowDialog();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();

        public void luu()
        {
            MyGetData(1);
            Huy_Text(this);
            //Flag = 0;
            txt_MaDG.Text = "Mã tự sinh";
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DocGia_Public dg = new DocGia_Public();
            if (txt_HoTen.Text != "" && cob_GioiTinh.Text != "" && cob_TenLop.Text != "" && txt_DiaChi.Text != "")
            {
                if (Flag == 0)
                {
                    //them
                    dg.HoVaTen = txt_HoTen.Text;
                    dg.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                    dg.GioiTinh = cob_GioiTinh.Text;
                    dg.DiaChi = txt_DiaChi.Text;
                    dg.MaLop = Convert.ToInt32(cob_MaLop.Text);
                    dg.DienThoai = txt_DienThoai.Text;
                    dg.Email = txt_Email.Text;
                    dg.NgayCap = Convert.ToDateTime(dateTime_NgayCap.Text);
                    docgia_bll.Insert_DocGia(dg, dg, dg, dg, dg, dg, dg, dg);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Thêm";
                    nk.MoTa = "Thêm mới Độc Giả: '" + txt_HoTen.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
                if (Flag == 1)
                {
                    //sua
                    dg.MaDocGia = int.Parse(txt_MaDG.Text);
                    dg.HoVaTen = txt_HoTen.Text;
                    dg.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                    dg.GioiTinh = cob_GioiTinh.Text;
                    dg.DiaChi = txt_DiaChi.Text;
                    dg.MaLop = Convert.ToInt32(cob_MaLop.Text);
                    dg.DienThoai = txt_DienThoai.Text;
                    dg.Email = txt_Email.Text;
                    dg.NgayCap = Convert.ToDateTime(dateTime_NgayCap.Text);
                    docgia_bll.Update_DocGia(dg, dg, dg, dg, dg, dg, dg, dg, dg);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Sửa";
                    nk.MoTa = "Sửa thông tin Độc Giả: '" + HoVaTen.ToString() + "' thành: '" + txt_HoTen.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                if (Flag == 2)
                {
                    //Xem
                    dg.MaDocGia = int.Parse(txt_MaDG.Text);
                    dg.HoVaTen = txt_HoTen.Text;
                    dg.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                    dg.GioiTinh = cob_GioiTinh.Text;
                    dg.DiaChi = txt_DiaChi.Text;
                    dg.MaLop = Convert.ToInt32(cob_MaLop.Text);
                    dg.DienThoai = txt_DienThoai.Text;
                    dg.Email = txt_Email.Text;
                    dg.NgayCap = Convert.ToDateTime(dateTime_NgayCap.Text);
                    docgia_bll.Update_DocGia(dg, dg, dg, dg, dg, dg, dg, dg, dg);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Sửa";
                    nk.MoTa = "Sửa thông tin Độc Giả: '" + HoVaTen.ToString() + "' thành: '" + txt_HoTen.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                luu();
            }
            else
            {
                XtraMessageBox.Show("Bạn chưa nhấp đủ dữ liệu!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }

        }


        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            txt_MaDG.Text = "Mã tự sinh";
            txt_MaDG.Enabled = false;
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            Flag = 4;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void cob_TenLop_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_Lop";
            dt = docgia_bll.TaoBang(sql);
            cob_MaLop.DataSource = dt;
            cob_MaLop.DisplayMember = "MaLop";
            cob_TenLop.DataSource = dt;
            cob_TenLop.DisplayMember = "TenLop";
        }

        private void cob_MaLop_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_Lop where MaLop='"+cob_MaLop.Text+"'";
                dt = docgia_bll.TaoBang(sql);
                cob_MaLop.DataSource = dt;
                cob_MaLop.DisplayMember = "MaLop";
                cob_TenLop.DataSource = dt;
                cob_TenLop.DisplayMember = "TenLop";
            }
            catch (Exception)
            { }
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            txt_MaDG.Text="Mã tự sinh";
            bbi_Them.Enabled = false ;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            this.Text = "Thêm Mới Độc Giả";
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag =1;
            txt_MaDG.Enabled = true;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            this.Text = "Sửa Độc Giả";
        }

        private void txt_MaDG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaDG_Leave(object sender, EventArgs e)
        {
            if (txt_MaDG.Text == "")
                txt_MaDG.Focus();
        }

        private void txt_HoTen_Leave(object sender, EventArgs e)
        {
            if (txt_HoTen.Text == "")
                txt_HoTen.Focus();
        }
    }
}